package com.br4mmie.asciistory;

import java.util.Date;

import com.br4mmie.asciistory.characters.Player;
import com.br4mmie.asciistory.graphics.WorldRenderer;
import com.br4mmie.asciistory.map.Map;
import com.br4mmie.asciistory.map.PlayerAvatar;

import android.app.*;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;

public class ASCIIStoryActivity extends Activity implements OnTouchListener
{
	private ProfileManager 	manager;
	private GameLoop 		game;
	private WorldRenderer	renderer;
	private boolean			running;
	
	private String			text;
	
	private CharImageDrawer interfaceDrawer;
	private CharImageDrawer gameDrawer;
	
	private TextView textView;
	
	public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        					WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        instance = this;
        
        interfaceDrawer = new CharImageDrawer()
        {
			public void draw(CharImage image) 
			{
				setText(image.toString());
			}
        };
        
        manager = new ProfileManager(null);
        game = new GameLoop(manager, interfaceDrawer);
        manager.setGameLoop(game);
        
        setContentView(R.layout.loadscreen);
        FileGetter.path = "/sdcard/ASCIIStory/";
        
        Thread gameLoopThread = 
        	new Thread(null, new GameLooper(this), "GameLoop", 0);
        gameLoopThread.start();
    }
	public boolean onTouch(View v, MotionEvent motion) 
	{
		int pressed = 0;
		if (motion.getAction() == MotionEvent.ACTION_DOWN) 	pressed = 1;
		if (motion.getAction() == MotionEvent.ACTION_UP) 	pressed = 2;
		
		Keybindings set = null;
		switch (v.getId())
		{
		case R.id.Btn_Close: set = Keybindings.CLOSE; break;
		case R.id.Btn_Down: set = Keybindings.DOWN; break;
		case R.id.Btn_Interact: set = Keybindings.INTERACT; break;
		case R.id.Btn_Inventory: set = Keybindings.INVENTORY; break;
		case R.id.Btn_Journal: set = Keybindings.JOURNAL; break;
		case R.id.Btn_Left: set = Keybindings.LEFT; break;
		case R.id.Btn_Right: set = Keybindings.RIGHT; break;
		case R.id.Btn_Status: set = Keybindings.STATUS; break;
		case R.id.Btn_Up: set = Keybindings.UP; break;
		case R.id.Btn_Use: set = Keybindings.INV_USE; break;
		case R.id.Btn_Eqp: set = Keybindings.INV_EQP; break;
		case R.id.Btn_Drop: set = Keybindings.INV_REM; break;
		}
		
		if (set != null && pressed > 0) set.setPressed(pressed == 1);
		
		return true;
	}
	
	private class GameLooper implements Runnable
	{
		private ASCIIStoryActivity activity;
		
		public GameLooper(ASCIIStoryActivity activity)
		{
			this.activity = activity;
		}
		public void run() 
		{
			game.startUp();
			
			activity.runOnUiThread
			(
				new Runnable() 
				{
					public void run()
					{
						activity.setContentView(R.layout.main);
						
						activity.findViewById(R.id.Btn_Up).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Down).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Left).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Right).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Interact).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Status).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Inventory).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Journal).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Close).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Use).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Eqp).setOnTouchListener(activity);
						activity.findViewById(R.id.Btn_Drop).setOnTouchListener(activity);
						
						textView = (TextView)activity.findViewById(R.id.screen);
						running = true;
					}
				}
			);
			while (!running);
			
			
			final long target_frametime = 300;
			long target_time = new Date().getTime() + target_frametime;
			while (running)
			{
				game.handleEvents();
				game.handleLoop();
				game.handleDraws();
				
				activity.runOnUiThread
				(
					new Runnable() 
					{
						public void run()
						{
							textView.setText(activity.text);
						}
					}
				);
				
				try 
				{
					long current_time = new Date().getTime();
					long sleep_time = target_time - current_time;
					
					if (sleep_time > 0) Thread.sleep(sleep_time);
					else				target_time = current_time;
				}
				catch (InterruptedException ex) {}
				target_time += target_frametime;
			}
			game.handleExit();
			activity.finish();
		}
	}

	public  static int keyDelay = 100;
	private static ASCIIStoryActivity instance;
	//getters
	public static int getScreenWidth()
	{
		return instance.game.getScreenWidth();
	}
	public static int getScreenHeight()
	{
		return instance.game.getScreenHeight();
	}
	public static Player getPlayer()
	{
		return instance.game.getPlayer();
	}
	public static Map getMap()
	{
		return instance.game.getMap();
	}
	public static PlayerAvatar getPlayerAvatar()
	{
		return instance.game.getAvatar();
	}
	public static ProfileManager getProfileManager()
	{
		return instance.manager;
	}
	public static GameLoop getGameLoop()
	{
		return instance.game;
	}
	
	//setters
	public static void setWorldRenderer(WorldRenderer renderer)
	{
		instance.renderer = renderer;
		instance.game.setRenderer(renderer);
	}
	public static void setRunning(boolean run)
	{
		instance.running = run;
	}
	public static void setText(String text)
	{
		instance.text = text;
	}
}
